package tienmanh.gs.goldminer.scene;

import java.util.ArrayList;
import java.util.Random;

import org.andengine.engine.camera.ZoomCamera;
import org.andengine.engine.camera.hud.HUD;
import org.andengine.engine.handler.timer.ITimerCallback;
import org.andengine.engine.handler.timer.TimerHandler;
import org.andengine.entity.particle.BatchedPseudoSpriteParticleSystem;
import org.andengine.entity.particle.emitter.RectangleParticleEmitter;
import org.andengine.entity.primitive.Line;
import org.andengine.entity.scene.IOnSceneTouchListener;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.scene.background.SpriteBackground;
import org.andengine.entity.sprite.Sprite;
import org.andengine.entity.text.Text;
import org.andengine.entity.text.TextOptions;
import org.andengine.input.touch.TouchEvent;
import org.andengine.util.HorizontalAlign;
import org.andengine.util.math.MathUtils;

import android.app.Dialog;
import android.preference.PreferenceManager;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.Toast;
import tienmanh.gs.goldminer.base.BaseScene;
import tienmanh.gs.goldminer.goldsoft.GameActivity;
import tienmanh.gs.goldminer.goldsoft.R;
import tienmanh.gs.goldminer.manager.ResourcesManager;
import tienmanh.gs.goldminer.manager.SceneManager;
import tienmanh.gs.goldminer.manager.SceneManager.SceneType;
import tienmanh.gs.goldminer.particle.BubbleAnimation;
import tienmanh.gs.goldminer.particle.SnowAnimation;
import tienmanh.gs.goldminer.sprite.Animal;
import tienmanh.gs.goldminer.sprite.Barie;
import tienmanh.gs.goldminer.sprite.Chest;
import tienmanh.gs.goldminer.sprite.Cua;
import tienmanh.gs.goldminer.sprite.Fish;
import tienmanh.gs.goldminer.sprite.Gold;
import tienmanh.gs.goldminer.sprite.Hook;
import tienmanh.gs.goldminer.sprite.Jewel;
import tienmanh.gs.goldminer.sprite.Miner;
import tienmanh.gs.goldminer.sprite.Mouse;
import tienmanh.gs.goldminer.sprite.MouseBeat;
import tienmanh.gs.goldminer.sprite.MyText;
import tienmanh.gs.goldminer.sprite.NgocTrai;
import tienmanh.gs.goldminer.sprite.Object;
import tienmanh.gs.goldminer.sprite.Pearl;
import tienmanh.gs.goldminer.sprite.Stones;
import tienmanh.gs.goldminer.sprite.TNT;
import tienmanh.gs.goldminer.sprite.Thunder;
import tienmanh.gs.goldminer.sprite.shop.Boom;
import tienmanh.gs.goldminer.sprite.shop.Clock;
import tienmanh.gs.goldminer.sprite.shop.FrzeeTime;
import tienmanh.gs.goldminer.sprite.shop.Strengh;
import tienmanh.gs.goldminer.util.Item;
import tienmanh.gs.goldminer.util.Level;
import tienmanh.gs.goldminer.util.MapObject;
import tienmanh.gs.goldminer.util.Star;
import tienmanh.gs.goldminer.util.Util;
import tienmanh.gs.goldminer.util.Value;

public class GameScene extends BaseScene {

	@Override
	public void createScene() {
		// TODO Auto-generated method stub
		initTimeHandler();
	    initValue();
	    creatGameScene();
	}

	@Override
	public void onBackKeyPressed() {
		// TODO Auto-generated method stub
		if(!isShowDialog)
       activity.runOnUiThread(new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				engine.stop();
			showDialogPause();	
			}
		});
	}

	@Override
	public SceneType getSceneType() {
		// TODO Auto-generated method stub
		return SceneType.SCENE_GAME;
	}

	@Override
	public void disposeScene() {
		// TODO Auto-generated method stub
		camera.setHUD(null);
	}

	@Override
	public void onPauseGame() {
		// TODO Auto-generated method stub
		engine.stop();
	}

	@Override
	public void onResumeGame() {
		// TODO Auto-generated method stub
		engine.stop();
		if(!isShowDialog)
		activity.runOnUiThread(new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
			showDialogPause();	
			}
		});
	}
	
	public boolean isShowDialog = false;
	
	public void showDialogPause(){
		final Dialog dialog = new Dialog(activity,android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);
		  isShowDialog = true;
		  dialog.setContentView(R.layout.pause);
		  dialog.setCancelable(false);
		Button thoat = (Button) dialog.findViewById(R.id.btn_thoat);
		thoat.setOnTouchListener(new OnTouchListener() {
			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
			    dialog.dismiss();
			    th.setAutoReset(false);
				th.reset();
				registerUpdateHandler(th);
				unregisterUpdateHandler(th);
				unregisterUpdateHandler(finishLevel);
			    SceneManager.getInstance().loadMenuScene(engine);
			    engine.start();
			    isShowDialog = false;
				return true;
			}
		});
		Button choilai = (Button) dialog.findViewById(R.id.btc_choilai);
		choilai.setOnTouchListener(new OnTouchListener() {
			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
			    dialog.dismiss();
			    th.setAutoReset(false);
				th.reset();
				registerUpdateHandler(th);
				unregisterUpdateHandler(th);
				unregisterUpdateHandler(finishLevel);
			    SceneManager.getInstance().loadShopScene(engine);
			    engine.start();
			    isShowDialog = false;
				return true;
			}
		});
		Button choitiep = (Button) dialog.findViewById(R.id.btn_choitiep);
		choitiep.setOnTouchListener(new OnTouchListener() {
			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				dialog.dismiss();
				engine.start();
				isShowDialog = false;
				return true;
			}
		});
		dialog.show();
	}
	
	//Loại bản đồ
		public boolean isMapTypeMiningStone;//loại bản đồ phá đưuọc đá
		
		public int goalGold;
		//thời gian khi ấn giữ màn hình
		 long timer;
		 
	//Kiểm tra trạng thái khi phá đá
		 public boolean isMiningStone;
		 public long valueMiningStone;
		 
//		 public boolean isCheck;
		 
		 
		public static boolean pullGold;
		
		
	  public static GameScene INSTACE;
	  public static ArrayList<Object> arrayGolds;
	  public static GameActivity goldActivity;
	  public static int height;
	  public static long money = 0;
	  public static HUD sceneHUD;
	  public static Text tvMoney;
	  public static int width;
	  private SpriteBackground bgGame;
//	  private SpriteBackground bgShop;
	  private Sprite btnBuyDiamond;
	  private Sprite btnBuyFlower;
	  private Sprite btnBuyMine;
	  private Sprite btnBuyTime;
	  private Sprite btnNextLevel;
	  private boolean buy = false;
	  public int codeBuy = 0;
	  private int countBezel = 0;
	  
	  tienmanh.gs.goldminer.util.Gold goldSave;
	  
	  private Sprite TimeFrame;
	//  private Explode explode;
	  TimerHandler finishLevel;
	  
	  //Khi gặp vật cản móc bị bật lại
	  public static boolean isPullAgain;
	  
	  //Kiểm tra khi người dùng sử dụng Item thuốc nổ
	  public boolean isUseMine = false;
	  
	  
	  private boolean inGame = false;
	  public boolean inPower = false;
	  
	  
	  private boolean isBuyDiamond = false;
	  private boolean isBuyFlower = false;
	  private boolean isBuyMine = false;
	  private boolean isBuyTime = false;
	  
	  
	  public boolean inExplode = false;
	  public static boolean isThunder;
	  
	  public boolean isFling;
	  public boolean isPull = false;
	  public boolean isUnPull = false;
	  
	  private Sprite ivGameBottom;
	  private boolean kq = false;
	  private int left = 0;
	  public int level;
	  
	  //Viên ngọc trai
	  public Pearl pearl;
	  
	  public static Line line1;
	//  public static Line line2;
	  
	  public static Hook hook1;
	//  public static Hook hook2;
	  
	  public static long mines = 0;
	  public static long times = 0;
	  public static long power = 0;
	  public static long frzze = 0;
	  
	  public static Miner miner;
	  public MyText myText;
	  private int right = 0;
	  TimerHandler splash;
	  public int t = 0;
	  TimerHandler th;
	  TimerHandler thThaiDo;
	  public int time = 10;
	//  private Text tvCountMine;
	  private int levelAll;
	  
	  private Text tvGoal;
	//  private Text tvGoalRequest;
	  private Sprite tableGoal; 
	  
	  //  private Text tvLevel;
	  private Text tvMess;
	  public static Text tvCountStreng;
	  public static Text tvCountFrzze;
	  public static Text tvCountMine;
	  public static Text tvCountTime;
	  public Text tvTime;
	  TimerHandler unLargeExplode;
	  TimerHandler unSmallExplode;
	  private Util util;
	  private Value value;
	  private int x;
	  private int y;
	  int bag;
	  
	  private Boom boomShop;
	  private Clock clockShop;
	  private FrzeeTime frzeeShop;
	  private Strengh strShop;
	  
	  //Giá trị lưu level vào file Xml
	  Level levelSP ;
	  
	  
	  //biến kiểm tra xem map nay Miner có được chạy không
	  public static boolean isMinerRunning;
	  
	  private void addEntity(int startX, int startY,  String paramString , float angle , int endX , int endY)
	  {
		  boolean huongdi = false;
		  if(endX!=0 && endY !=0){
			  if(endX > startX) huongdi=true;
		  
			  if(endX < startX) huongdi = false;
		  }
	    if (paramString.equals(this.value.TYPE_GOLD_0))
	    {
	      Gold localGolds1 = new Gold(startX,startY, this.value.WIDTH_GOLD_0, this.value.HEIGHT_GOLD_0, this.resourcesManager.textureRegionGold0, this.vbom, this.value.GOLD_5, this.value.SPEED_GOLD_0, paramString);
	      sceneHUD.attachChild(localGolds1);
	      arrayGolds.add(localGolds1);
	    }
	    
	    if (paramString.equals(this.value.TYPE_GOLD_1))
	    {
	    	Gold localGolds2 = new Gold(startX,startY, this.value.WIDTH_GOLD_1, this.value.HEIGHT_GOLD_1, this.resourcesManager.textureRegionGold1, this.vbom, this.value.GOLD_4, this.value.SPEED_GOLD_1, paramString);
	      sceneHUD.attachChild(localGolds2);
	      arrayGolds.add(localGolds2);
	    }
	    
	    if (paramString.equals(this.value.TYPE_GOLD_2))
	    {
	    	Gold localGolds3 = new Gold(startX,startY, this.value.WIDTH_GOLD_2, this.value.HEIGHT_GOLD_2, this.resourcesManager.textureRegionGold2, this.vbom, this.value.GOLD_3, this.value.SPEED_GOLD_2, paramString);
	      sceneHUD.attachChild(localGolds3);
	      arrayGolds.add(localGolds3);
	    }
	    
	    if (paramString.equals(this.value.TYPE_GOLD_3))
	    {
	    	Gold localGolds4 = new Gold(startX,startY, this.value.WIDTH_GOLD_3, this.value.HEIGHT_GOLD_3, this.resourcesManager.textureRegionGold3, this.vbom, this.value.GOLD_2, this.value.SPEED_GOLD_3, paramString);
	      sceneHUD.attachChild(localGolds4);
	      arrayGolds.add(localGolds4);
	    }
	    
	    if (paramString.equals(this.value.TYPE_GOLD_4))
	    {
	    	Gold localGolds4 = new Gold(startX,startY, this.value.WIDTH_GOLD_4, this.value.HEIGHT_GOLD_4, this.resourcesManager.textureRegionGold4, this.vbom, this.value.GOLD_1, this.value.SPEED_GOLD_3, paramString);
	      sceneHUD.attachChild(localGolds4);
	      arrayGolds.add(localGolds4);
	    }
	    
	    if (paramString.equals(this.value.TYPE_GOLD_5))
	    {
	    	Gold localGolds4 = new Gold(startX,startY, this.value.WIDTH_GOLD_5, this.value.HEIGHT_GOLD_5, this.resourcesManager.textureRegionGold5, this.vbom, this.value.GOLD_0, this.value.SPEED_GOLD_3, paramString);
	      sceneHUD.attachChild(localGolds4);
	      arrayGolds.add(localGolds4);
	    }
	    
	    if (paramString.equals(this.value.TYPE_JEWEL_0))
	    {
	    	Jewel localGolds4 = new Jewel(startX,startY, this.value.WIDTH_JEWEL0, this.value.HEIGHT_JEWEL0, this.resourcesManager.textureRegionJewel0, this.vbom, this.value.JEWEL_0, this.value.SPEED_GOLD_3, paramString,0);
	      sceneHUD.attachChild(localGolds4);
	      arrayGolds.add(localGolds4);
	    }
	    if (paramString.equals(this.value.TYPE_JEWEL_1))
	    {
	    	Jewel localGolds4 = new Jewel(startX,startY, this.value.WIDTH_JEWEL0, this.value.HEIGHT_JEWEL0, this.resourcesManager.textureRegionJewel0, this.vbom, this.value.JEWEL_1, this.value.SPEED_GOLD_3, paramString,1);
	      sceneHUD.attachChild(localGolds4);
	      arrayGolds.add(localGolds4);
	    }
	    if (paramString.equals(this.value.TYPE_JEWEL_2))
	    {
	    	Jewel localGolds4 = new Jewel(startX,startY, this.value.WIDTH_JEWEL0, this.value.HEIGHT_JEWEL0, this.resourcesManager.textureRegionJewel0, this.vbom, this.value.JEWEL_2, this.value.SPEED_GOLD_3, paramString,2);
	      sceneHUD.attachChild(localGolds4);
	      arrayGolds.add(localGolds4);
	    }
	    if (paramString.equals(this.value.TYPE_JEWEL_3))
	    {
	    	Jewel localGolds4 = new Jewel(startX,startY, this.value.WIDTH_JEWEL0, this.value.HEIGHT_JEWEL0, this.resourcesManager.textureRegionJewel0, this.vbom, this.value.JEWEL_3, this.value.SPEED_GOLD_3, paramString,3);
	      sceneHUD.attachChild(localGolds4);
	      arrayGolds.add(localGolds4);
	    }
	    if (paramString.equals(this.value.TYPE_JEWEL_4))
	    {
	    	Jewel localGolds4 = new Jewel(startX,startY, this.value.WIDTH_JEWEL1, this.value.HEIGHT_JEWEL1, this.resourcesManager.textureRegionJewel1, this.vbom, this.value.JEWEL_4, this.value.SPEED_GOLD_3, paramString,0);
	      sceneHUD.attachChild(localGolds4);
	      arrayGolds.add(localGolds4);
	    }
	    if (paramString.equals(this.value.TYPE_JEWEL_5))
	    {
	    	Jewel localGolds4 = new Jewel(startX,startY, this.value.WIDTH_JEWEL1, this.value.HEIGHT_JEWEL1, this.resourcesManager.textureRegionJewel1, this.vbom, this.value.JEWEL_5, this.value.SPEED_GOLD_3, paramString,1);
	      sceneHUD.attachChild(localGolds4);
	      arrayGolds.add(localGolds4);
	    }
	    if (paramString.equals(this.value.TYPE_JEWEL_6))
	    {
	    	Jewel localGolds4 = new Jewel(startX,startY, this.value.WIDTH_JEWEL1, this.value.HEIGHT_JEWEL1, this.resourcesManager.textureRegionJewel1, this.vbom, this.value.JEWEL_5, this.value.SPEED_GOLD_3, paramString,2);
	      sceneHUD.attachChild(localGolds4);
	      arrayGolds.add(localGolds4);
	    }
	    if (paramString.equals(this.value.TYPE_JEWEL_7))
	    {
	    	Jewel localGolds4 = new Jewel(startX,startY, this.value.WIDTH_JEWEL1, this.value.HEIGHT_JEWEL1, this.resourcesManager.textureRegionJewel1, this.vbom, this.value.JEWEL_7, this.value.SPEED_GOLD_3, paramString,3);
	      sceneHUD.attachChild(localGolds4);
	      arrayGolds.add(localGolds4);
	    }
	    if (paramString.equals(this.value.TYPE_JEWEL_8))
	    {
	    	Jewel localGolds4 = new Jewel(startX,startY, this.value.WIDTH_JEWEL2, this.value.HEIGHT_JEWEL2, this.resourcesManager.textureRegionJewel2, this.vbom, this.value.JEWEL_8, this.value.SPEED_GOLD_3, paramString,0);
	      sceneHUD.attachChild(localGolds4);
	      arrayGolds.add(localGolds4);
	    }
	    if (paramString.equals(this.value.TYPE_JEWEL_9))
	    {
	    	Jewel localGolds4 = new Jewel(startX,startY, this.value.WIDTH_JEWEL2, this.value.HEIGHT_JEWEL2, this.resourcesManager.textureRegionJewel2, this.vbom, this.value.JEWEL_9, this.value.SPEED_GOLD_3, paramString,1);
	      sceneHUD.attachChild(localGolds4);
	      arrayGolds.add(localGolds4);
	    }
	    if (paramString.equals(this.value.TYPE_JEWEL_10))
	    {
	    	Jewel localGolds4 = new Jewel(startX,startY, this.value.WIDTH_JEWEL2, this.value.HEIGHT_JEWEL2, this.resourcesManager.textureRegionJewel2, this.vbom, this.value.JEWEL_10, this.value.SPEED_GOLD_3, paramString,2);
	      sceneHUD.attachChild(localGolds4);
	      arrayGolds.add(localGolds4);
	    }
	    if (paramString.equals(this.value.TYPE_JEWEL_11))
	    {
	    	Jewel localGolds4 = new Jewel(startX,startY, this.value.WIDTH_JEWEL2, this.value.HEIGHT_JEWEL2, this.resourcesManager.textureRegionJewel2, this.vbom, this.value.JEWEL_11, this.value.SPEED_GOLD_3, paramString,3);
	      sceneHUD.attachChild(localGolds4);
	      arrayGolds.add(localGolds4);
	    }
	    
	    if (paramString.equals(this.value.TYPE_STONE_0))
	    {
	      Stones stone = new Stones(startX,startY, this.value.WIDTH_STONE_0, this.value.HEIGHT_STONE_0, this.resourcesManager.textureRegionStone0, this.vbom, this.value.STONE_0, this.value.SPEED_STONE_0, paramString);
	      sceneHUD.attachChild(stone);
	      arrayGolds.add(stone);
	    }
	    
	    if (paramString.equals(this.value.TYPE_STONE_1))
	    {
	    	Stones stone1 = new Stones(startX,startY, this.value.WIDTH_STONE_01, this.value.HEIGHT_STONE_01, this.resourcesManager.textureRegionStone01, this.vbom, this.value.STONE_01, this.value.SPEED_STONE_0, paramString);
	      sceneHUD.attachChild(stone1);
	      arrayGolds.add(stone1);
	    }
	    
	    if (paramString.equals(this.value.TYPE_STONE_2))
	    {
	    	Stones stone1 = new Stones(startX,startY, this.value.WIDTH_STONE_02, this.value.HEIGHT_STONE_02, this.resourcesManager.textureRegionStone02, this.vbom, this.value.STONE_02, this.value.SPEED_STONE_0, paramString);
	      sceneHUD.attachChild(stone1);
	      arrayGolds.add(stone1);
	    }
	    
	    if (paramString.equals(this.value.TYPE_STONE_3))
	    {
	    	Stones stone1 = new Stones(startX,startY, this.value.WIDTH_STONE_1, this.value.HEIGHT_STONE_1, this.resourcesManager.textureRegionStone1, this.vbom, this.value.STONE_1, this.value.SPEED_STONE_1, paramString);
	      sceneHUD.attachChild(stone1);
	      arrayGolds.add(stone1);
	    }
	    
	    if (paramString.equals(this.value.TYPE_NGOCTRAIDEN))
	    {
	    	NgocTrai ngoctrai = new NgocTrai(startX,startY, this.value.WIDTH_STONE_0, this.value.HEIGHT_STONE_0, this.resourcesManager.textureRegionNgocTrai, this.vbom, this.value.NGOCTRAI, this.value.SPEED_NGOCTRAI, paramString,0);
	        sceneHUD.attachChild(ngoctrai);
	         arrayGolds.add(ngoctrai);
	    }
	    
	    if (paramString.equals(this.value.TYPE_NGOCTRAITRANG))
	    {
	    	NgocTrai ngoctrai = new NgocTrai(startX,startY, this.value.WIDTH_STONE_0, this.value.HEIGHT_STONE_0, this.resourcesManager.textureRegionNgocTrai, this.vbom, this.value.NGOCTRAI, this.value.SPEED_NGOCTRAI, paramString,1);
	        sceneHUD.attachChild(ngoctrai);
	         arrayGolds.add(ngoctrai);
	    }
	    
	    if (paramString.equals(this.value.TYPE_CHEST))
	    {
	      Chest bag = new Chest(startX,startY, this.value.WIDTH_CHEST, this.value.HEIGHT_CHEST, this.resourcesManager.textureRegionChest, this.vbom, this.value.BAG, this.value.SPEED_CHEST, paramString);
	      sceneHUD.attachChild(bag);
	      arrayGolds.add(bag);
	    }
	    
	    if (paramString.equals(this.value.TYPE_TNT))
	    {
	      TNT localGolds9 = new TNT(startX, startY, this.value.WIDTH_TNT, this.value.HEIGHT_TNT, this.resourcesManager.textureRegionTNT, this.vbom, this.value.TNT, this.value.SPEED_TNT, paramString);
	      sceneHUD.attachChild(localGolds9);
	      arrayGolds.add(localGolds9);
	    }
	    
	    if (paramString.equals(this.value.TYPE_BEZEL_PIG_1))
	    {
	      Animal mouse1 = new Animal(startX,startY, this.value.WIDTH_BEZEL_PIG, this.value.HEIGHT_BEZEL_PIG, this.resourcesManager.ttrMouse1, this.vbom, value.MOUSE_0, this.value.SPEED_BEZEL_PIG, this.value.TYPE_BEZEL_PIG_1);
	      mouse1.pigRun(startX,startY, endX, endY, 1,huongdi);
	      sceneHUD.attachChild(mouse1);
	      arrayGolds.add(mouse1);
	    }
	    
	    if (paramString.equals(this.value.TYPE_BEZEL_PIG_2))
	    {
	      Mouse mouse2 = new Mouse(startX,startY, this.value.WIDTH_BEZEL_PIG1, this.value.HEIGHT_BEZEL_PIG1, this.resourcesManager.ttrMouse2, this.vbom, value.MOUSE_1, this.value.SPEED_BEZEL_PIG, this.value.TYPE_BEZEL_PIG_2);
	      mouse2.pigRun(startX,startY,  endX, endY, 2,huongdi);
	      sceneHUD.attachChild(mouse2);
	      arrayGolds.add(mouse2);
	    }
	    
	    if (paramString.equals(this.value.TYPE_BEZEL_PIG_3))
	    {
	      Animal mouse3 = new Animal(startX,startY, this.value.WIDTH_BEZEL_PIG, this.value.HEIGHT_BEZEL_PIG, this.resourcesManager.ttrMouse3, this.vbom, value.MOUSE_2, this.value.SPEED_BEZEL_PIG, this.value.TYPE_BEZEL_PIG_3);
	      mouse3.pigRun(startX,startY, endX, endY, this.value.SPEED_BEZEL,huongdi);
	      sceneHUD.attachChild(mouse3);
	      arrayGolds.add(mouse3);
	    }
	    
	    if (paramString.equals(this.value.TYPE_BEZEL_PIG_4))
	    {
	      Animal mouse4 = new Animal(startX,startY, this.value.WIDTH_BEZEL_PIG, this.value.HEIGHT_BEZEL_PIG, this.resourcesManager.ttrMouse4, this.vbom, value.MOUSE_3, this.value.SPEED_BEZEL_PIG, this.value.TYPE_BEZEL_PIG_4);
	      mouse4.pigRun(startX,startY,  endX, endY, this.value.SPEED_BEZEL,huongdi);
	      sceneHUD.attachChild(mouse4);
	      arrayGolds.add(mouse4);
	    }
	    
	    if (paramString.equals(this.value.TYPE_BEZEL_PIG_5))
	    {
	      MouseBeat mouse4 = new MouseBeat(startX,startY, this.value.WIDTH_BEZEL_PIGBEAT, this.value.HEIGHT_BEZEL_PIGBEAT, this.resourcesManager.ttrMouseBeat, this.vbom, value.MOUSE_4, this.value.SPEED_BEZEL_PIG, this.value.TYPE_BEZEL_PIG_5);
	      mouse4.pigRun(startX,startY,  endX, endY, 1,huongdi);
	      sceneHUD.attachChild(mouse4);
	      arrayGolds.add(mouse4);
	    }
	    
	    if (paramString.equals(this.value.TYPE_ELECTRICFISH))
	    {
	      Fish mouse4 = new Fish(startX,startY, this.value.WIDTH_ELEFISH, this.value.HEIGHT_ELEFISH, this.resourcesManager.ttrElecFish, this.vbom, value.FISH_E, this.value.SPEED_BEZEL_PIG, this.value.TYPE_ELECTRICFISH);
	      mouse4.pigRun(startX,startY,  endX, endY, this.value.SPEED_BEZEL,huongdi);
	      sceneHUD.attachChild(mouse4);
	      arrayGolds.add(mouse4);
	    }
	    if (paramString.equals(this.value.TYPE_FISH1))
	    {
	      Fish mouse4 = new Fish(startX,startY, this.value.WIDTH_FISH1, this.value.HEIGHT_FISH1, this.resourcesManager.ttrFish1, this.vbom, value.FISH_0, this.value.SPEED_BEZEL_PIG, this.value.TYPE_FISH1);
	      mouse4.pigRun(startX,startY,  endX, endY, this.value.SPEED_BEZEL,huongdi);
	      sceneHUD.attachChild(mouse4);
	      arrayGolds.add(mouse4);
	    }
	    if (paramString.equals(this.value.TYPE_FISH2))
	    {
	      Fish mouse4 = new Fish(startX,startY, this.value.WIDTH_FISH2, this.value.HEIGHT_FISH2, this.resourcesManager.ttrFish2, this.vbom, value.FISH_1, this.value.SPEED_BEZEL_PIG, this.value.TYPE_FISH2);
	      mouse4.pigRun(startX,startY,  endX, endY, this.value.SPEED_BEZEL,huongdi);
	      sceneHUD.attachChild(mouse4);
	      arrayGolds.add(mouse4);
	    }
	    if (paramString.equals(this.value.TYPE_FISH3))
	    {
	      Cua mouse4 = new Cua(startX,startY, this.value.WIDTH_FISH3, this.value.HEIGHT_FISH3, this.resourcesManager.ttrFish3, this.vbom, value.FISH_21, this.value.SPEED_BEZEL_PIG, this.value.TYPE_FISH3);
	      mouse4.pigRun(startX,startY,  endX, endY, 10);
	      sceneHUD.attachChild(mouse4);
	      arrayGolds.add(mouse4);
	    }
	    if (paramString.equals(this.value.TYPE_FISH4))
	    {
	    	Cua mouse4 = new Cua(startX,startY, this.value.WIDTH_FISH4, this.value.HEIGHT_FISH4, this.resourcesManager.ttrFish4, this.vbom, value.FISH_2, this.value.SPEED_BEZEL_PIG, this.value.TYPE_FISH4);
	      mouse4.pigRun(startX,startY,  endX, endY, 10);
	      sceneHUD.attachChild(mouse4);
	      arrayGolds.add(mouse4);
	    }
	    if (paramString.equals(this.value.TYPE_FISH5))
	    {
	    	Cua mouse4 = new Cua(startX,startY, this.value.WIDTH_FISH5, this.value.HEIGHT_FISH5, this.resourcesManager.ttrFish5, this.vbom, value.FISH_3, this.value.SPEED_BEZEL_PIG, this.value.TYPE_FISH5);
	      mouse4.pigRun(startX,startY,  endX, endY, this.value.SPEED_BEZEL_PIG);
	      sceneHUD.attachChild(mouse4);
	      arrayGolds.add(mouse4);
	    }
	    
	    if (paramString.equals(this.value.TYPE_BARIE))
	    {
	      Barie barie  = new Barie(startX,startY, this.value.WIDTH_BARIE, this.value.HEIGHT_BARIE, this.resourcesManager.textureRegionBarier, this.vbom, 50, this.value.SPEED_BEZEL_PIG, this.value.TYPE_BARIE,angle);
	      barie.Run(startX,startY,  endX, endY, 5);
	      sceneHUD.attachChild(barie);
	      arrayGolds.add(barie);
	    }
	  }

	  private void clearScreen()
	  {
	    clearChildScene();
	    clearEntityModifiers();
	    sceneHUD.detachChildren();
	    clearTouchAreas();
	    clearUpdateHandlers();
	  }

	  private void gameUpdate()
	  {
	    this.engine.unregisterUpdateHandler(this.th);
	    this.engine.registerUpdateHandler(this.th);
	  }

	  public static GameScene getInstace()
	  {//
	    return INSTACE;
	  }
	  
	  private MapObject mapOBJ;

	  public void addMap()
	  {
		  for (int i = 0; i < mapOBJ.getObj().size(); i++) {
			  addEntity(mapOBJ.getObj().get(i).getPoitX(), mapOBJ.getObj().get(i).getPointY(), value.getType(Integer.parseInt(mapOBJ.getObj().get(i).getId().trim())),new Random().nextInt(90),mapOBJ.getObj().get(i).getPoitEndX(),mapOBJ.getObj().get(i).getPointEndY());
		  }
	      arrayGolds.add(new TNT(0, 0, 0, 0, resourcesManager.textureRegionChest, vbom, 0, 0, ""));//Giá trị cho vào để không bị lỗi
	  }

	  public void creatGameScene()
	  {
	    sceneHUD = new HUD();
	    width = 800;
	    height = 480;
	    INSTACE = this;
	    goldActivity = this.resourcesManager.activity;
	    this.engine = this.resourcesManager.engine;
	    initia4Game();
	    camera.setHUD(sceneHUD);
	  }
	  

	  public void initTimeHandler()
	  {
	    this.splash = new TimerHandler(0.01F, false, new ITimerCallback()
	    {
	      public void onTimePassed(TimerHandler paramTimerHandler)
	      {
	    	  mapOBJ = new MapObject();
	    	  try {
	    			mapOBJ = new Value().getObjectInMap(level);
	    		} catch (Exception e) {
	    			// TODO Auto-generated catch block
	    			e.printStackTrace();
	    		}
	    	  time = mapOBJ.getTime();
	    	  goalGold = mapOBJ.getRequire();  
	        GameScene.this.setLevel();
	      }
	    });
	    
	    this.finishLevel = new TimerHandler(0.1F, false, new ITimerCallback()
	    {
	      public void onTimePassed(TimerHandler paramTimerHandler)
	      {
	    	  th.reset();
	  		  unregisterUpdateHandler(th);
	  		  long valueGold = money;
	  		  if(level >= levelAll) {
	  			  levelAll++;
	  		   	levelSP.writeLevel(levelAll);
	  		  }
	  		  level++;
	  		  goldSave.writeGoldSP(valueGold);
	  		  levelSP.writeGoldSP(level);
	  		  levelSP.writeStateRun(true);
	    	  SceneManager.getInstance().loadMapScene(engine);
	      }
	    });
	    
	    
	    
	    this.th = new TimerHandler(1.0F, true, new ITimerCallback()
	    {
	      public void onTimePassed(TimerHandler paramTimerHandler)
	      {
	        GameScene localGameScene1 = GameScene.this;
	        if(!isFrzee)
	        localGameScene1.time--;
	        GameScene.this.tvTime.setText("" + GameScene.this.time);
	        System.out.println(time);
	        if (GameScene.this.time == 0)
	        {
	          GameScene.this.inGame = false;
	          if (GameScene.money < GameScene.this.util.getGold(GameScene.this.level))
	          {
	        	  GameScene.this.kq = false;
	          if (PreferenceManager.getDefaultSharedPreferences(GameScene.goldActivity.getBaseContext()).getBoolean("music", false))
	              GameScene.this.resourcesManager.musicBackground.pause();
	            GameScene.this.loadScreenFinishLevel(true);
	          }
	          else
	          {
//	          level++;
	          GameScene.this.kq = true;
	          GameScene.this.loadScreenFinishLevel(false);
	          }
	          
	        }
	        }
	      
	    });
	  }

	  public void initValue()
	  {
		  goldSave = new tienmanh.gs.goldminer.util.Gold();
		  levelSP = new Level();
		  goldSave.init();
		  gold.init();
		  star.init();
		  item.init();
		  levelSP.init();
		    
		  money = gold.readEGoldSP();
		  isShowDialog = false;
		  isPullAgain = false;
		  isThunder = false;
		  isTouch = false;
		  this.codeBuy = 0;
		  this.countBezel = 0;
		  this.inExplode = false;
		  this.inGame = false;
		  this.inPower = false;
		  this.isBuyDiamond = false;
		  this.isBuyFlower = false;
		  this.isBuyMine = false;
		  this.isBuyTime = false;
		  this.isFling = false;
		  this.isPull = false;
		  this.isUnPull = false;
		  this.kq = false;
		  levelAll = levelSP.readLevel();
		  this.level = levelSP.readEGoldSP();
		  this.money = goldSave.readEGoldSP();
		  this.power = 0;
		  this.right = 0;
	    
	    this.time = 60;
	    
	    this.t = 0;
	    isMinerRunning = true;
	  }

	  public void initia4Game()
	  {
//	    this.level = 1;
	    this.time = 60;
	    this.times = 0;
	    this.inGame = false;
	    this.isFling = false;
	    this.isPull = false;
	    this.isUnPull = false;
//	    this.isFling2 = false;
//	    this.isPull2 = false;
//	    this.isUnPull = false;
	    this.inPower = false;
	    this.kq = false;
	    
	    this.util = new Util();
	    this.value = new Value();
	    this.ivGameBottom = new Sprite(0.0F, 0.0F, this.resourcesManager.textureRegionBackground, this.vbom);
	    this.bgGame = new SpriteBackground(this.ivGameBottom);
	    GameScene.miner = new Miner(-15 + width / 2, 26.0F, value.WWIDTH_MINER,value.HEIGHT_MINER, this.resourcesManager.ttrMiner, this.vbom);
//	    miner.minerRun(miner.getX(), miner.getY(), 500, miner.getY(), 5.0f);
	    hook1 = new Hook(miner.getX()+70, miner.getY()+30, 30.0F, 30.0F, this.resourcesManager.ttrHook, this.vbom, 800, 480,line1);
//	    GameScene.hook2 = new Hook(miner.getX()+130, miner.getY()+30, 30.0F, 30.0F, this.resourcesManager.ttrHook, this.vbom, 800, 480,line2);
	    
	    
	    this.tvTime = new Text(220, 25.0F, this.resourcesManager.font1, goldActivity.getBaseContext().getResources().getString(R.string.time) + ":60", 15, this.vbom);
	    this.tvTime.setColor(1.0F, 1.0F, 0.0F);
	    
	    this.TimeFrame = new Sprite(210, 20, resourcesManager.ttrTime, vbom);
	    this.TimeFrame.setScale(1.3f);
	    
	    this.tvMess = new Text(150.0F, -100 + height, this.resourcesManager.font1, goldActivity.getBaseContext().getResources().getString(R.string.mes_win), 200, this.vbom);
	    this.tvMess.setColor(1.0F, 1.0F, 0.0F);
	    tvMoney = new Text(50.0F, 2.0F, this.resourcesManager.font1, "$0", 15, this.vbom);
	    tvMoney.setColor(1.0F, 1.0F, 0.0F);
	    tvMoney.setSize(20.0F, 100.0F);
	    
	    tableGoal = new Sprite(0, 0, resourcesManager.textureRegionBAG, vbom);
	    
	    this.tvGoal = new Text(50.0F, 42.0F, this.resourcesManager.font1,"$750", 15, this.vbom);
	    this.tvGoal.setColor(1.0F, 1.0F, 0.0F);
	    this.tvCountFrzze = new Text(-170 + width, 0.0F, this.resourcesManager.font1, "", 15, this.vbom);
	    this.tvCountFrzze.setColor(1.0F, 0.0F, 0.0F);
	    this.tvCountMine = new Text(-120 + width, 0.0F, this.resourcesManager.font1, "", 15, this.vbom);
	    this.tvCountMine.setColor(1.0F, 0.0F, 0.0F);
	    this.tvCountStreng = new Text(-70 + width, 0.0F, this.resourcesManager.font1, "", 15, this.vbom);
	    this.tvCountStreng.setColor(1.0F, 0.0F, 0.0F);
	    this.tvCountTime = new Text(-20 + width, 0.0F, this.resourcesManager.font1, "", 15, this.vbom);
	    this.tvCountTime.setColor(1.0F, 0.0F, 0.0F);
	    final TimerHandler localTimerHandler = new TimerHandler(4.0F, false, new ITimerCallback()
	    {
	      public void onTimePassed(TimerHandler paramTimerHandler)
	      {
	        GameScene.sceneHUD.detachChild(GameScene.this.tvMess);
	      }
	    });
	    boomShop = new Boom(0, 0,50,50, resourcesManager.textureRegionItemTNT, vbom,null,null);
	    frzeeShop = new FrzeeTime(0, 0,50,50, resourcesManager.textureRegionItemICE, vbom,null,null);
	    clockShop = new Clock(0, 0,50,50 ,resourcesManager.textureRegionItemCLOCK, vbom,null,null);
	    strShop = new Strengh(0, 0,50,50, resourcesManager.textureRegionItemMINE, vbom,null,null);
	    GameScene.line1 = new Line((width / 2 )+50, 60.0F, -20 + width / 2, 45.0F, 2.0F, this.vbom);
	    GameScene.line1.setColor(0.1F, 0.1F, 0.1F);
	    
//	    GameScene.line2 = new Line(width / 2, 60.0F, -20 + width / 2, 45.0F, 2.0F, this.vbom);
//	    GameScene.line2.setColor(0.1F, 0.1F, 0.1F);
	    loadScreenSplash();
	  }

	  public void loadScreenFinishLevel(boolean paramBoolean)
	  {
	    clearScreen();
//	    if (this.level > this.value.MAX_LEVEL)
//	    {
//	      Bundle localBundle = new Bundle();
//	      localBundle.putBoolean("kt", true);
//	      localBundle.putInt("score", money);
//	      goldActivity.finish();
//	    }
//	    if (PreferenceManager.getDefaultSharedPreferences(goldActivity.getBaseContext()).getBoolean("sound", true))
//	      this.resourcesManager.getSoundEndLevel().play();
//	    setBackground(this.bgScreenGoal);
//	    this.tvGoalRequest.setPosition(-90 + width / 2, height / 2);
//	    if (paramBoolean)
//	    {
//	      this.tvGoalRequest.setText(goldActivity.getBaseContext().getResources().getString(R.string.goal));
//	      this.tvGoalRequest.setColor(1.0F, 1.0F, 0.0F);
//	    }
//	    else{
//	    	this.tvGoalRequest.setText(goldActivity.getBaseContext().getResources().getString(R.string.goal));
//	        this.tvGoalRequest.setColor(1.0F, 1.0F, 0.0F);
//	    }
//	    while (true)
//	    {
	      this.finishLevel.reset();
	      this.engine.unregisterUpdateHandler(this.finishLevel);
	      this.engine.registerUpdateHandler(this.finishLevel);
//	      return;
	      
//	    }
	  }


	  public void loadScreenSplash()
	  {
	    clearScreen();
////	    if (PreferenceManager.getDefaultSharedPreferences(goldActivity.getBaseContext()).getBoolean("sound", true))
////	      this.resourcesManager.getSoundStartLevel().play();
////	    setBackground(this.bgScreenGoal);
////	    this.tvGoalRequest.setPosition(-85 + width / 2, height / 2);
////	    if (this.util.getGold(this.level) < 1000)
////	      this.tvGoalRequest.setText("$00" + this.util.getGold(this.level));
////	    if (this.util.getGold(this.level) > 10000)
////	    {
////	      this.tvGoalRequest.setText("$" + this.util.getGold(this.level));
////	    }
////	    else
////	    this.tvGoalRequest.setText("$0" + this.util.getGold(this.level));
////	    sceneHUD.attachChild(this.tvGoalRequest);
	    this.splash.reset();
	    unregisterUpdateHandler(this.splash);
	    registerUpdateHandler(this.splash);
	  }



	  public void resetLevel()
	  {
		  pullGold = false;
	    this.time = 60;
	    this.buy = false;
	    this.inGame = true;
	    this.isFling = false;
	    this.isPull = false;
	    this.isUnPull = false;
	    this.inPower = false;
	    this.kq = false;
	    this.left = 0;
	    this.right = 0;
	    this.countBezel = 0;
	    GameScene.hook1.reset();
//	    GameScene.hook2.reset();
	    clearScreen();
	  }

	  public void setLevel()
	  {
	    clearScreen();
	    resetLevel();
	    GameScene.hook1 = new Hook(miner.getX()+50, miner.getY()+90, 30.0F, 30.0F, this.resourcesManager.ttrHook, this.vbom, width, height,line1);
	    initItem();
	    gameUpdate();
	    arrayGolds = new ArrayList();
	    setBackground(this.bgGame);
	    sceneHUD.attachChild(tableGoal);
	    sceneHUD.attachChild(GameScene.miner);
	    sceneHUD.attachChild(TimeFrame);
	    this.tvTime.setText(""+this.time);
	    sceneHUD.attachChild(this.tvTime);
	    this.tvGoal.setText("$" + goalGold);
	    sceneHUD.attachChild(this.tvGoal);
	    tvMoney.setText( "$" + money);
	    sceneHUD.attachChild(tvMoney);
	    sceneHUD.attachChild(GameScene.line1);
	    sceneHUD.attachChild(GameScene.hook1);
	    tvCountFrzze.setText(""+frzze);
	    sceneHUD.attachChild(this.tvCountFrzze);
	    tvCountMine.setText(""+mines);
	    sceneHUD.attachChild(this.tvCountMine);
	    tvCountStreng.setText(""+power);
	    sceneHUD.attachChild(this.tvCountStreng);
	    tvCountTime.setText(""+times);
	    sceneHUD.attachChild(this.tvCountTime);
	    GameScene.line1.setLineWidth(2.8f);
	    addMap();
	    sceneHUD.attachChild(new SnowAnimation(
				new RectangleParticleEmitter(400, 0, 800, 1),
				2, 5, 100, resourcesManager.mSnowParticleRegion, 
				vbom));
	    
	    processGame(hook1,line1);
	   
	        sceneHUD.setOnSceneTouchListener(new IOnSceneTouchListener()
	        {
	        	@SuppressWarnings("static-access")
				@Override
	          public boolean onSceneTouchEvent(Scene paramScene, TouchEvent paramTouchEvent)
	          {
//	        		System.out.println(GameScene.this.isFling+" "+isPull+"  "+pullGold+" " + "  "+isTouch);
	        		if(!isTouch)
//	        		{
	            if ((!GameScene.this.isFling) && (!GameScene.this.isPull)  && (!GameScene.pullGold) && paramTouchEvent.getY() > 60 )
	            {
//	            	if(paramTouchEvent.getAction() == TouchEvent.ACTION_DOWN){
//	            		timer = System.currentTimeMillis();
//	            		isMiningStone = true;
//	             	   System.out.println("VÀo đây này Down");
//	            	}
//	            }
//	        		if(paramTouchEvent.getAction() == TouchEvent.ACTION_UP){
//	            		isMiningStone = false;
	            	    isTouch = true;
//	            		if(System.currentTimeMillis() - timer > 250){
//	            			 touchScreen(valueMiningStone);
//	            		}
//	            		else{
	            			touchScreen(250,hook1);
//	            		}
	            		timer=0;
	            		valueMiningStone = 0;
	            		GameScene.hook1.unRotate();
	             		GameScene.this.isFling = true;
	             	    GameScene.this.isUnPull = false;
	             	    GameScene.this.isPull = false;
	             	    if(isMinerRunning) miner.unMinerRun();
	            	}
//	           }
	            return false;
	          }
	        });
	  }
	  
	  public boolean isTouch;
	  private Thunder thunder ;
	  TimerHandler thunder_Hander = new TimerHandler(3f,false,new ITimerCallback() {
		
		@Override
		public void onTimePassed(TimerHandler pTimerHandler) {
			// TODO Auto-generated method stub
			System.out.println("Thunder");
			sceneHUD.detachChild(thunder);
			isThunder = false;
			pullHookState();	
		}
	});
	  
	  //Xử lý sự kiện đóng băng
	  public static boolean isFrzee = false;
	  
	  public TimerHandler dongbang = new TimerHandler(15.5f,false,new ITimerCallback() {
			
			@Override
			public void onTimePassed(TimerHandler pTimerHandler) {
				// TODO Auto-generated method stub
				isFrzee = false;
				for (int i = 0; i < arrayGolds.size()-1; i++) {
					if(arrayGolds.get(i)!=null)
						arrayGolds.get(i).isFrzzeIce = false;
				}
			    valueFrzzeTime1 = 0;
			}
		});
	  
	  public void waitingThrowHook(Hook hook){
		  isPearl = false;
		  pearl = null;
		  hook.setCurrentTileIndex(0);
		  GameScene.this.isPullAgain = false;
			GameScene.this.isFling = false;
			isTouch = false;
			 if(isMinerRunning) miner.MinerRun();
			 
		 miner.unAnimateThrow();
	  }
	  public void touchScreen(long value , Hook hook){
	      hook.flingHook(hook.getX(),hook.getY(),value);
	  }
	  
	  public void pullHookState(){
		  miner.setAnimateThrow();
		  GameScene.this.isFling = false;
	      GameScene.this.isUnPull = true;
	      GameScene.this.isPull = true;
	  }
	  public int random(boolean lucky){
			int i = 0;
//		   if(lucky){
//			   i = new Random().nextInt(2);
//			  return i;
//		   }
//		   else{
			   i = new Random().nextInt(5);
			      
//		   }
		   return i;
		}
	  
	  public static tienmanh.gs.goldminer.util.Gold gold;
	  public static Star star;
	  public static Item item;
	  
	  //Khởi tạo các vật phẩm
	  public void initItem(){
		    
		  
	        this.clockShop.setPosition(-50 + width, 15.0F);
	        sceneHUD.registerTouchArea(clockShop);
	        sceneHUD.attachChild(this.clockShop);
	        
	        
	        this.strShop.setPosition(-100 + width, 15.0F);
	        sceneHUD.attachChild(this.strShop);
	        sceneHUD.registerTouchArea(strShop);
	        
	        
	        this.boomShop.setPosition(-150 + width, 15.0F);
	        sceneHUD.attachChild(this.boomShop);
	        sceneHUD.registerTouchArea(this.boomShop);
	        
	        
	        this.frzeeShop.setPosition(-200 + width, 15.0F);
	        sceneHUD.attachChild(this.frzeeShop);
	        sceneHUD.registerTouchArea(frzeeShop);
	        
	        mines = item.readItemSP2();
	        frzze = item.readItemSP1();
	        power = item.readItemSP4();
	        times = item.readItemSP3();
	        
	  }
	  
	  //Xóa các vật phẩm bên trong tâm vụ nổ
	  int values;
	  ArrayList<Integer> a = new ArrayList<Integer>();
	  public void removeItemMine(Object e , int exp){
		  
		  a= new ArrayList<Integer>();
	           for (int i = 0; i < arrayGolds.size()-1; i++) {
	        	   if(exp != i)
	        		   if(GameScene.arrayGolds.get(i)!=null)
	        			   	if(e.collidesWith(arrayGolds.get(i))){
	        			   		if(!arrayGolds.get(i).getType().equalsIgnoreCase(value.TYPE_TNT))
	        			   		{
	        			   			if(arrayGolds.get(i).getType().equals(new Value().TYPE_BEZEL_PIG_2))
	        			   				arrayGolds.get(i).detachMouseGold();
	        			   			sceneHUD.detachChild(arrayGolds.get(i));
	        			   			GameScene.arrayGolds.set(i, null);
	        			   		}
	        			   		else
	        			   		{
	        			   	     values = i;
	        			   	     System.out.println(i);
	        			   	     a.add(i);
	        			   	     arrayGolds.get(values).setScale(1.5f);
	        	  			     arrayGolds.get(values).setWidth(150);
	        	  			     arrayGolds.get(values).setHeight(150);
	        	  			     arrayGolds.get(values).isExplode = true;
	        	  			     GameScene.arrayGolds.get(values).animate(new long[]{100,100,100,100,100}, new int[]{1,2,3,4,5},false);
//	        	  			     arrayGolds.get(values).stt = i;
//	        			   			removeItemMine(arrayGolds.get(i),i);
	        			   		}
				  }
			}	  
	           arrayGolds.get(exp).stt = exp;
	           GameScene.arrayGolds.set(exp, null);
	           if(a.size()>0)
	           {
	           removeItemMine(arrayGolds.get(a.get(0)),a.get(0));
	           }
	  }
	  
	  //Giá trị khi add băng
	  public boolean isFrzzeIce;
	  public long valueFrzzeTime1;
	  
	  
	  public void processGame(final Hook hook , final Line line){
		  sceneHUD.registerUpdateHandler(new TimerHandler(0.01f, true,new ITimerCallback() {
				
				@Override
				public void onTimePassed(TimerHandler pTimerHandler) {
					// TODO Auto-generated method stub
//					if(isMinerRunning){
					 hook.rXo = miner.getX()+30;
			         hook.rYo = miner.getY()+45;
//					}
//			         System.out.println(isFrzee +" - "+ isFrzzeIce);
			         
//			         updateMinerPosition();
			         //Bắt sự kiện khi người chơi thả móc
			          if (GameScene.this.isFling)
			          {
			          	for (int i = 0; i < arrayGolds.size()-1; i++) 
			          	{
			          		if(GameScene.arrayGolds.get(i)!=null && GameScene.arrayGolds.get(i).isVisible())
			          		if (hook.collidesWith(GameScene.arrayGolds.get(i)))
			                  {
			                    if ((GameScene.arrayGolds.get(i)).getType().equals(GameScene.this.value.TYPE_ELECTRICFISH) && !isFrzee)
			                    {
			                    	if(!isThunder){
			                    	thunder = new Thunder(line.getX1(), line.getY1(), resourcesManager.textureRegionThunder, vbom,line,hook);
			                    	sceneHUD.attachChild(thunder);
			                    	thunder_Hander.reset();
			                    	engine.unregisterUpdateHandler(thunder_Hander);
			                    	engine.registerUpdateHandler(thunder_Hander);
			                    	hook.unFlingHook();
			                    	isThunder = true;
			                    	}
			                    }
			                    else
			                    	if((GameScene.arrayGolds.get(i)).getType().equals(GameScene.this.value.TYPE_STONE_3) || (GameScene.arrayGolds.get(i)).getType().equals(GameScene.this.value.TYPE_BARIE) && !isFrzee ){
			                    		pullHookState();
			                    	}
			                    else 
			                    	if((GameScene.arrayGolds.get(i)).getType().equals(GameScene.this.value.TYPE_NGOCTRAIDEN) || (GameScene.arrayGolds.get(i)).getType().equals(GameScene.this.value.TYPE_NGOCTRAITRANG) && !isFrzee){
//			                    		System.out.println(GameScene.arrayGolds.get(i).ispullAgain);
			                    		if(GameScene.arrayGolds.get(i).isIspullAgainPearl())
			                    		{
			                    			GameScene.arrayGolds.get(i).ngoctraiUP(2);
			                    			if((GameScene.arrayGolds.get(i)).getType().equals(GameScene.this.value.TYPE_NGOCTRAIDEN))
			                    			 collisionPullPearl(i,hook,line,0);
			                    			else
			                    				collisionPullPearl(i,hook,line,1);
			                    		}
			                    		else
			                    			pullHookState();
			                    	}
			                    else
			                    {
			                   if ((GameScene.arrayGolds.get(i)).getType().equals(GameScene.this.value.TYPE_TNT) && !isFrzee){
			                	     arrayGolds.get(i).setWidth(100);
			                	     arrayGolds.get(i).setHeight(100);
			                	     arrayGolds.get(i).setScale(1.5f);
			                	     arrayGolds.get(i).isExplode = true;
			                         showExplode(i,true);
			                	     pullHookState();
//			                	     arrayGolds.get(i).animate(new long[]{100,100,100,100,100}, new int[]{1,2,3,4,5},false);
//			                	     arrayGolds.set(i, null);
			                        }
			                   else
			                  	    collisionPullObject(i,hook,line);
			                    }
			                  break;
			                  }
			          	}
			          	 if ((!GameScene.this.isPull) && ((hook.getX() > GameScene.width+30) || (hook.getY() > GameScene.height+30) || (hook.getX() <= 0.0F)))
			             {
			              pullHookState();
			             }
			        }
			         

			          //Bắt sự kiện khi người chơi kéo móc
			        if (GameScene.this.isPull)
			         { 
			        	if (!GameScene.this.inExplode)
			            {
			            if ((hook.getY() <= hook.rYo) && (!GameScene.this.isUnPull))
			            {
			            	miner.stopAnimation();
			            	if(isPearl){
			          			GameScene.this.myText = new MyText(hook.getX(), hook.getY(), GameScene.this.resourcesManager.font1, "$" + pearl.getGold(), 20, new TextOptions(HorizontalAlign.LEFT), GameScene.this.vbom, 30.0F + GameScene.tvMoney.getX(), GameScene.tvMoney.getY(), 6, GameScene.this.t,line,hook);
				                GameScene.sceneHUD.attachChild(GameScene.this.myText);
				                money+=pearl.getGold();
			          		 }
			            	else
			            	 if(arrayGolds.get(t) == null)
			          		  {
			            		 GameScene.getInstace().isPull = false;
			 				    hook.setVisible(true);
			 				    hook.unPullHook();
			 					hook.Rotate();
			 					GameScene.miner.stopAnimation();
			 					if(GameScene.isMinerRunning)  GameScene.miner.MinerRun();
			 					GameScene.pullGold = false;
			           			GameScene.getInstace().isTouch = true;
			 					GameScene.getInstace().waitingThrowHook(hook);
			                  }
			                 else
			          	  if(!arrayGolds.get(t).getType().equalsIgnoreCase(value.TYPE_CHEST))
			          	  {
			                 GameScene.this.myText = new MyText(hook.getX(), hook.getY(), GameScene.this.resourcesManager.font1, "$" + (GameScene.arrayGolds.get(GameScene.this.t)).getGold(), 20, new TextOptions(HorizontalAlign.LEFT), GameScene.this.vbom, 30.0F + GameScene.tvMoney.getX(), GameScene.tvMoney.getY(), 6, GameScene.this.t,line,hook);
			                 GameScene.sceneHUD.attachChild(GameScene.this.myText);
			                 money+=GameScene.arrayGolds.get(GameScene.this.t).getGold();
			                 
			          	  }
			          	  else
			          	  {
			          		bag = random(true);
			          		System.out.println("BAG : "+ bag);
			          		switch (bag) {
								case 0:
									int money = MathUtils.random(10, 300);
									GameScene.this.myText = new MyText(hook.getX(), hook.getY(), GameScene.this.resourcesManager.font1, "$" + money, 20, new TextOptions(HorizontalAlign.LEFT), GameScene.this.vbom, 30.0F + GameScene.tvMoney.getX(), GameScene.tvMoney.getY(), 6, GameScene.this.t,line,hook);
					                GameScene.sceneHUD.attachChild(GameScene.this.myText);
					                GameScene.money+=money;
									break;
								case 1:
									 GameScene.this.boomShop = new Boom(hook.getX(), hook.getY(),resourcesManager.textureRegionItemTNT.getWidth()/2,resourcesManager.textureRegionItemTNT.getHeight()/2, resourcesManager.textureRegionItemTNT, vbom,line,hook);
									 GameScene.sceneHUD.attachChild(GameScene.this.boomShop);
									 boomShop.moveText(-150 + width, 15.0F, t);
									break;
								case 2:
									GameScene.this.strShop = new Strengh(hook.getX(), hook.getY(),resourcesManager.textureRegionItemMINE.getWidth()/2,resourcesManager.textureRegionItemMINE.getHeight()/2, resourcesManager.textureRegionItemMINE, vbom,line,hook);
									GameScene.sceneHUD.attachChild(GameScene.this.strShop);
									strShop.moveText(-100 + width, 15.0F, t);
									break;
								case 3:
									GameScene.this.frzeeShop = new FrzeeTime(hook.getX(), hook.getY(),resourcesManager.textureRegionItemICE.getWidth()/2,resourcesManager.textureRegionItemICE.getHeight()/2, resourcesManager.textureRegionItemICE, vbom,line,hook);
									GameScene.sceneHUD.attachChild(GameScene.this.frzeeShop);
									frzeeShop.moveText(-200 + width, 15.0F, t);
									break;
								case 4:
									GameScene.this.clockShop = new Clock(hook.getX(), hook.getY(),resourcesManager.textureRegionItemCLOCK.getWidth()/2,resourcesManager.textureRegionItemCLOCK.getHeight()/2, resourcesManager.textureRegionItemCLOCK, vbom,line,hook);
									GameScene.sceneHUD.attachChild(GameScene.this.clockShop);
									clockShop.moveText(-50 + width, 15.0F, t);
									break;

								default:
									break;
								}
			          	  }
			          	  GameScene.this.isPull = false;
			              GameScene.miner.stopAnimation();
//			              GameScene.arrayGolds.get(t).stopAnimation();
			            }
			            else
			            	if ((hook.getY() <= 100.0F) && (GameScene.this.isUnPull))
			            		{
			            	    	miner.stopAnimation();
			            			hook.unPullHook();
			            			hook.Rotate();
			            			miner.stopAnimation();
			            			GameScene.this.isPull = false;
			            			GameScene.this.isUnPull = false;
			            			GameScene.this.isFling = false;
			            			if(isUseMine) {
			            				isUseMine = false;
			            			}
			            			hook.unFlingHook();
			            			pullGold = false;
//			            			 isTouch = false;
			            			waitingThrowHook(hook);
			            		}
			            }
			            else
			            {
			            	miner.stopAnimation();
			          	  GameScene.this.isUseMine = true;
			              GameScene.this.isUnPull = true;
			              GameScene.this.inExplode = false;
			              float[] arrayOfFloat = hook.convertLocalToSceneCoordinates(10, 0.0F);
			              float f1 = arrayOfFloat[0];
			              float f2 = arrayOfFloat[1];
//			              hook.pullHookAfterMine(f1, f2);
			              hook.unPullHook();
			              hook.pullHook(hook.getX(), hook.getY(), 300F);
			            }
			          }
			        
			        
			          if (GameScene.this.isUnPull)
			            {
			              hook.pullHook(hook.getX(), hook.getY(), 300F);
			              hook.unFlingHook();
//			              GameScene.miner.setAnimatePull(0.0F);
			              GameScene.this.isPull = true;
			            }
			          
			          //Khi người chơi giữ chạt màn hình
			          if(isMiningStone){
			          	if(valueMiningStone < 1500){
			              valueMiningStone = System.currentTimeMillis() - timer;
//			              tvCountMine.setText(""+valueMiningStone);
			          	}
			          }
			          if(!isFling && !isPull && !pullGold) isTouch = false;
				}
			}));
	  }
	  
	  public void showExplode(int i , boolean check){
		  if(check) GameScene.arrayGolds.get(i).animate(new long[]{100,100,100,100,100}, new int[]{1,2,3,4,5},false);
		   removeItemMine(arrayGolds.get(i), i);
	  }
	  
	  public void collisionPullObject(int i , Hook hook , Line line){
		  hook.unFlingHook();
		  miner.setAnimateThrow();
	      GameScene.sceneHUD.detachChild(GameScene.arrayGolds.get(i));
	      GameScene.sceneHUD.attachChild(GameScene.arrayGolds.get(i));
	      GameScene.sceneHUD.detachChild(line);
	      GameScene.sceneHUD.attachChild(line);
	      GameScene.sceneHUD.detachChild(hook);
	      GameScene.sceneHUD.attachChild(hook);
	      pullGold = true;
	      GameScene.this.t = i;
//	      GameScene.miner.setAnimatePull((GameScene.arrayGolds.get(i)).getSpeed());
	      GameScene.this.isFling = false;
	      GameScene.this.isPull = true;
	      GameScene.arrayGolds.get(i).setPosition(GameScene.this.util.getCenterX(hook) - (GameScene.arrayGolds.get(i)).getWidth() / 2.0F, GameScene.this.util.getCenterY(hook));
	      if(inPower){
	      	hook.pullHook(hook.getX(), hook.getY(), (GameScene.arrayGolds.get(i)).getSpeed()*2);
	      	GameScene.arrayGolds.get(i).pullBackGold(hook.rXo- (hook.getX() - (GameScene.arrayGolds.get(i)).getX()), hook.rYo - (hook.getY() - (GameScene.arrayGolds.get(i)).getY()), (GameScene.arrayGolds.get(i)).getX(), (GameScene.arrayGolds.get(i)).getY(), true);
	      }
	      else
	      {
	      	hook.pullHook(hook.getX(), hook.getY(), (GameScene.arrayGolds.get(i)).getSpeed());
	      	GameScene.arrayGolds.get(i).pullBackGold(hook.rXo - (hook.getX() - (GameScene.arrayGolds.get(i)).getX()), hook.rYo  - (hook.getY() - (GameScene.arrayGolds.get(i)).getY()), (GameScene.arrayGolds.get(i)).getX(), (GameScene.arrayGolds.get(i)).getY(), false);
	      }
	      arrayGolds.get(i).setRotationCenter(arrayGolds.get(i).getWidth() / 2.0F,0);
	      arrayGolds.get(i).setRotation(hook.angle - 90.0F);
	  }
	  
	  //Kiểm tra trạng thái khi câu trúng ngọc trai
	  public boolean isPearl;
	  
	  
	  public void collisionPullPearl(int i , Hook hook , Line line,int value){
		  isPearl = true;
		  hook.unFlingHook();
		  miner.setAnimateThrow();
	      pullGold = true;
	      GameScene.this.isFling = false;
	      GameScene.this.isPull = true;
	      pearl = new Pearl(0,0,resourcesManager.textureRegionPearl.getWidth(),resourcesManager.textureRegionPearl.getHeight(), resourcesManager.textureRegionPearl, vbom,1000,this.value.SPEED_NGOCTRAI,new Value().TYPE_NGOCTRAIDEN);
	      sceneHUD.attachChild(pearl);
	      pearl.setCurrentTileIndex(value);
	      pearl.setPosition(GameScene.this.util.getCenterX(hook) - pearl.getWidth() / 2.0F - 10, GameScene.this.util.getCenterY(hook)-5);
	      GameScene.sceneHUD.detachChild(GameScene.arrayGolds.get(i));
	      GameScene.sceneHUD.attachChild(GameScene.arrayGolds.get(i));
	      GameScene.sceneHUD.detachChild(line);
	      GameScene.sceneHUD.attachChild(line);
	      GameScene.sceneHUD.detachChild(hook);
	      GameScene.sceneHUD.attachChild(hook);
	      if(inPower){
	      	hook.pullHook(hook.getX(), hook.getY(), (GameScene.arrayGolds.get(i)).getSpeed()*2);
	      	pearl.pullBackGold(hook.rXo - (hook.getX() - pearl.getX()), hook.rYo - (hook.getY() - pearl.getY()), pearl.getX(), pearl.getY(), true);
	      }
	      else
	      {
	      	hook.pullHook(hook.getX(), hook.getY(), (GameScene.arrayGolds.get(i)).getSpeed());
	      	pearl.pullBackGold(hook.rXo - (hook.getX() - pearl.getX()), hook.rYo  - (hook.getY() - pearl.getY()), pearl.getX(), pearl.getY(), false);
	      }
	      pearl.setRotationCenter(pearl.getWidth() / 2.0F, 1.0F);
	      pearl.setRotation(hook.angle - 90.0F);
	  }

}
